#include <QCoreApplication>
#include <vector>
#include <iostream>
#include <string>

using namespace std;

void pr_3_14()
{
    vector<int> ivec;
    int i;
    char cont = 'y';
    while (cin >> i)
    {
        ivec.push_back(i);
        cout << "您要继续么？(y or n)?" << endl;
        cin >> cont;
        if (cont != 'y' && cont != 'Y')
        {
            break;
        }
    }
    for (auto mem : ivec)
    {
        cout << mem << endl;
    }
}

void pr_3_15()
{
    vector<string> svec;
    string s;
    char cont = 'y';
    while (cin >> s)
    {
        svec.push_back(s);
        cout << "您要继续么？(y or n)?" << endl;
        cin >> cont;
        if (cont != 'y' && cont != 'Y')
        {
            break;
        }
    }
    for (auto &mem : svec)
    {
        cout << mem << " ";
    }
}

void pr_3_16()
{
    vector<int> v1;
    vector<int> v2(10);
    vector<int> v3(10, 42);
    vector<int> v4{10};
    vector<int> v5{10, 42};
    vector<int> v6{10};
    vector<string> v7{10, "hi"};

    cout << "v1 size " << v1.size() << endl;
    for (auto mem : v1) {
        cout << mem;
    }
    cout << endl;

    cout << "v2 size " << v2.size() << endl;
    for (auto mem : v2) {
        cout << mem;
    }
    cout << endl;

    cout << "v3 size " << v3.size() << endl;
    for (auto mem : v3) {
        cout << mem;
    }
    cout << endl;

    cout << "v4 size " << v4.size() << endl;
    for (auto mem : v4) {
        cout << mem;
    }
    cout << endl;

    cout << "v5 size " << v5.size() << endl;
    for (auto mem : v5) {
        cout << mem;
    }
    cout << endl;

    cout << "v6 size " << v6.size() << endl;
    for (auto mem : v6) {
        cout << mem;
    }
    cout << endl;

    cout << "v7 size " << v7.size() << endl;
    for (auto mem : v7) {
        cout << mem;
    }
    cout << endl;
}

void pr_3_17()
{
    vector<string> vString;
    string s;
    char cont = 'y';
    cout << "请输入需要转换的词组:" << endl;
    while (cin >> s)
    {
        vString.push_back(s);
        cout << "keep input? (y or n)" << endl;
        cin >> cont;
        if (cont != 'y')
        {
            break;
        }
    }

    for (auto &mem : vString)
    {
        for (auto &c : mem)
        {
            c = toupper(c);
        }
        cout << mem << endl;
    }
}

int pr_3_20_1()
{
    vector<int> vInt;
    int iVal;
    cout << "please input values:" << endl;
    while (cin >> iVal)
    {
        vInt.push_back(iVal);
    }
    if (vInt.size() == 0)
    {
        cout << "none elements in vector!!!" << endl;
        return -1;
    }
    cout << "cla 1:" << endl;
    for (decltype(vInt.size()) i = 0; i < vInt.size() - 1; i += 2)
    {
        cout << vInt[i] + vInt[i + 1] << " "; // 计算相邻元素的和
        if ((i + 2) % 10 == 0) // 每行输出5个数字
        {
            cout << endl;
        }
    }
    // 如果元素数是奇数，需要特殊处理最后一个元素
    if ((vInt.size() % 2) != 0)
    {
        cout << vInt[vInt.size() - 1];
    }
    return 0;
}

int pr_3_20_2()
{
    vector<int> vInt;
    int iVal;
    cout << "please input values:" << endl;
    while (cin >> iVal)
    {
        vInt.push_back(iVal);
    }
    // 首尾元素和
    cout << "首尾元素的和依次是：" << endl;
    if (vInt.size() == 0)
    {
        cout << "none elements in vector!!!" << endl;
        return -1;
    }
    for (decltype(vInt.size()) i = 0; i < vInt.size() / 2; i++)
    {
        cout << vInt[i] + vInt[vInt.size() - 1 - i] << " ";
        if ((i + 2) % 10 == 0)
        {
            cout << endl;
        }
    }
    // 如果元素数是奇数，需要特殊处理最后一个元素
    if ((vInt.size() % 2) != 0)
    {
        cout << vInt[vInt.size() / 2];
    }
    return 0;
}

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    pr_3_20_2();


    return a.exec();
}
